package com.yinhai.message.core.dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.api.bean.TMMessage;
import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.po.MessageDetailPo;
import com.yinhai.message.core.util.IMessageConstant;

/**
 * 
 * @author yanglq 2017.3.4
 *
 */
@Repository("messageDetailDao")
public class MessageDetailDaoImpl extends HibernateDAO<MessageDetailPo> implements IMessageDetailDao {
	
	@Override
	public MessageDetailPo save(MessageDetailPo po){
		super.save(po);
		return po;
	}

	@Override
	public void batchSave(final List<MessageDetailPo> detailPos) {

		super.jdbcTemplate.batchUpdate("INSERT  INTO  TAMESSAGEDETAIL (mgid,userid,systemid,sendertype,senddate,state) VALUES (?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				MessageDetailPo messageDetailPo = detailPos.get(i);
				ps.setString(1, messageDetailPo.getId().getMgid());
				ps.setString(2, messageDetailPo.getId().getUserid());
				ps.setString(3, messageDetailPo.getId().getSystemid());
				ps.setString(4, messageDetailPo.getId().getSendertype());
				ps.setDate(5,new Date(messageDetailPo.getSenddate().getTime()));
				ps.setString(6, messageDetailPo.getState());
			}

			@Override
			public int getBatchSize() {
				return detailPos.size();
			}
		});
	}

	@Override
	public void update(MessageDetailPo po) {
		super.update(po);
	}

	@Override
	public void updateBatch(final List<MessageDetailPo> poList) {
//		super.updateAll(poList);
		super.jdbcTemplate.batchUpdate("update TAMESSAGEDETAIL set senddate=?,state=?,errorinfo=? where mgid=? and userid=? and systemid=? and sendertype=? ", new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				MessageDetailPo messageDetailPo = poList.get(i);
				ps.setDate(1, new Date(new java.util.Date().getTime()));
				ps.setString(2, messageDetailPo.getState());
				ps.setString(3, messageDetailPo.getErrorinfo());
				ps.setString(4, messageDetailPo.getId().getMgid());
				ps.setString(5, messageDetailPo.getId().getUserid());
				ps.setString(6, messageDetailPo.getId().getSystemid());
				ps.setString(7, messageDetailPo.getId().getSendertype());
			}

			@Override
			public int getBatchSize() {
				return poList.size();
			}
		});
	}
	
	@Override
	public List<TMMessageVo> queryErrorMessageDetails() {
		StringBuilder sql = new StringBuilder();
		sql.append("select  d.mgid, d.userid, d.systemid, d.sendertype, d.appid, ")
			.append("c.tel,c.email,c.phoneapp,c.imid, ")
			.append("u.name, m.suserid, m.ssystemid, m.title, m.mgcontent ")
			.append("from tamessagedetail d,tamessage m,tausercontact c, tauser u ")
			.append("where d.state='").append(IMessageConstant.MSG_STATE_SEND_FAIL).append("' and d.resend='0' ")
			.append("and d.mgid = m.mgid ")
			.append("and d.userid = c.userid ")
			.append("and m.suserid = u.userid ");
		
		return jdbcTemplate.query(sql.toString(), new RowMapper<TMMessageVo>(){

			@Override
			public TMMessageVo mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				TMMessageVo vo = new TMMessageVo();
				TMUser suser = new TMUser();
				TMUser user = new TMUser();
				List<TMUser> receiveUserList = new ArrayList<>();
				List<String> messageTypes = new ArrayList<>();
		    	TMMessage message = new TMMessage();
		    	//构建TMMessageVo
		    	user.setUserId(rs.getString("userid"));
				user.setAppId(rs.getString("systemid"));
				user.setSms(rs.getString("tel"));
				user.setEmail(rs.getString("email"));
				user.setRegistrationId(rs.getString("phoneapp"));
				user.setImId(rs.getString("imid"));
				receiveUserList.add(user);
				suser.setAppId(rs.getString("ssystemid"));
				suser.setUserId(rs.getString("suserid"));
				suser.setName(rs.getString("name"));
				message.setMgid(rs.getString("mgid"));
				message.setContent(rs.getString("mgcontent"));
				message.setTitle(rs.getString("title"));
				messageTypes.add(rs.getString("sendertype"));
				message.setMessageTypes(messageTypes);
				message.setScriptMethod(rs.getString("appid"));
				vo.setMessage(message);
				vo.setUser(user);
				vo.setUserlist(receiveUserList);
				return vo;
			}
			
		});
	}

	@Override
	public void updateBatchByReSend(final List<MessageDetailPo> poList) {
		super.jdbcTemplate.batchUpdate("update TAMESSAGEDETAIL set resend=1, state=?,errorinfo=errorinfo||'--2:'||? where mgid=? and userid=? and systemid=? and sendertype=? ", new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				MessageDetailPo messageDetailPo = poList.get(i);
				ps.setString(1, messageDetailPo.getState());
				ps.setString(2, messageDetailPo.getErrorinfo());
				ps.setString(3, messageDetailPo.getId().getMgid());
				ps.setString(4, messageDetailPo.getId().getUserid());
				ps.setString(5, messageDetailPo.getId().getSystemid());
				ps.setString(6, messageDetailPo.getId().getSendertype());
			}

			@Override
			public int getBatchSize() {
				return poList.size();
			}
		});
	}
}
